package cn.zzf.core;
import cn.zzf.po.TeacherInfo;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import java.io.*;
import java.util.*;

/**
 * 课表数据解析
 *
 * @author GaoFeng2017
 * @date 2018-02-14 11:04:50
 **/

public class LessonParse extends DataParse {
    private Logger logger = LogManager.getLogger(this.getClass());
    public LessonParse(File dataFileRoot) {
        super(dataFileRoot);
    }

    /**
     * @description: 解析教师信息
     * @author: GaoFeng2017
     * @date: 2018/2/14 12:58
     * @param: []
     * @return: java.util.List<cn.zzf.po.TeacherInfo>
     *
     */
    public List<TeacherInfo> teacherInfoParse() {
        if (this.dataFiles != null && !this.dataFiles.isEmpty()) {
            List<TeacherInfo> list = new ArrayList<>(dataFiles.size());
            int count = 0;
            for (Properties p : dataFiles) {
                TeacherInfo teacherInfo = new TeacherInfo();
                teacherInfo.setDepartment(p.getProperty("depart"));
                teacherInfo.setName(p.getProperty("name"));
                teacherInfo.setGmtCreate(new Date());
                teacherInfo.setSex(p.getProperty("sex"));
                count++;
                teacherInfo.setNumber(Long.parseLong("1" +  CrawerUtils.getFillZero(5 - (count + "").length()) + count)) ;
                teacherInfo.setPassword("1" + p.getProperty("tid"));
                list.add(teacherInfo);
            }

            return  list;
        }

        return  null;
    }

    private List<Map<String,String[]>> teacherLessonSplit() {
        if (this.dataFiles != null && !this.dataFiles.isEmpty()) {
            List<Map<String,String[]>> list = new ArrayList<>(dataFiles.size());
            String[] week = {"星期一","星期二","星期三","星期四","星期五","星期六","星期日"};
            for (Properties p : dataFiles) {
                Map<String,String[]> map = new LinkedHashMap<>();
                for(String s : week) {
                    String[] lesson = p.getProperty(s).split(",");
                    if (!Objects.equals(lesson[0],"")) {
                        map.put(s,lesson);
                    }
                }
                list.add(map);
            }
            return  list;
        }
        return null;
    }

    public List<List<LessonItem>> teacherLessonParse() {
        List<Map<String,String[]>> contents = teacherLessonSplit();
        List<List<LessonItem>> lessonItems = new ArrayList<>();
        for (Map<String,String[]> map : contents) {
            Set<Map.Entry<String, String[]>> entries = map.entrySet();
            List<LessonItem> itemList = new ArrayList<>(map.size());
            for (Map.Entry<String,String[]> me : entries) {
                String week = me.getKey();
                String[] lessons = me.getValue();
                for (String lesson : lessons) {
                    LessonItem lessonItem = lessonItemParse(lesson,week);
                    if (lessonItem == null) {
                        continue;
                    }
                    lessonItem.setWeekday(week);
                    itemList.add(lessonItem);
                }
            }

            lessonItems.add(itemList);
        }

        return lessonItems;
    }

    /**
     * @description:
     * @author: GaoFeng2017
     * @date: 2018/2/14 17:15
     * @param: [lesson]
     * @return: cn.zzf.po.LessonItem
     *
     */
    private LessonItem lessonItemParse(String lesson,String week) {
//        lesson = lesson.replaceAll("(人数：\\d+)\\s?","");
        String[] items = lesson.split("!!");
        LessonItem lessonItem = new LessonItem();
        if (items.length < 4) {
            logger.info(week + "::" + lesson);
            logger.info("课程信息严重缺失");

            if (items[0].contains("体育")) {
                lessonItem.setLessonName(items[0]);
                lessonItem.setWeekInfo(items[1]);
                lessonItem.setPeriod(items[2]);
                lessonItem.setCampus("混合校区");
                lessonItem.setClassroomAndClass("田径场(待定)");
                logger.info(week + "已处理");
                return lessonItem;
            }


        }

        lessonItem.setLessonName(items[0]);

        if (!items[1].contains("[")) {
            logger.info(week + "::" + lesson);
            logger.info("周次不匹配");
            return null;
        }

        lessonItem.setWeekInfo(items[1]);

        if (!items[2].contains("节")) {
            logger.info("节次不匹配");
            return null;
        }

        lessonItem.setPeriod(items[2]);

        if (!items[3].contains("校区")) {
            lessonItem.setCampus("混合校区");

            if (items[0].contains("体育")) {
                lessonItem.setClassroomAndClass("田径场(待定) " + items[3]);
            } else {
                lessonItem.setClassroomAndClass("教室信息缺失 " + items[3]);
            }

            return lessonItem;
        } else {
            lessonItem.setCampus(items[3]);
        }

        lessonItem.setClassroomAndClass(items[4]);
        return  lessonItem;
    }


}
